/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package management;

import entity.ManipulationFrame;
import java.awt.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author
 */
public class CheckForm {
    // check textfield khong duoc trong

    public static boolean isEmpty(String value) {
        if (value.length() == 0) {
            return false;
        } else {
            return true;
        }
    }
    static Pattern pt = null;
    static Matcher mt = null;
    //check Phone is value

    public static boolean isNumber(String value) {
        // pt = Pattern.compile("[0]{1}[1,9]{1}[0-9]{8,9}");
        pt = Pattern.compile("\\d+");
        mt = pt.matcher(value);
        if (!mt.find()) {
            return false;
        } else {
            return true;
        }
    }

    //check Email is valid
    public static boolean isEmail(String value) {
        pt = Pattern.compile("[a-zA-Z][_]*\\d*@[a-z]{2,5}[.][a-z]{2,3}");
        mt = pt.matcher(value);
        if (!mt.find()) {
            return false;
        } else {
            return true;
        }
    }

    //Check Name is valid
    public static boolean isName(String value) {
        pt = Pattern.compile("[^[A-Za-z ]{5,50}]");
        mt = pt.matcher(value);
        if (!mt.find()) {
            return false;
        } else {
            return true;
        }
    }

    //Check Address is valid
    public static boolean isAddress(String value) {
        pt = Pattern.compile("[^[a-zA-Z0-9 ]{2,20}]");
        mt = pt.matcher(value);
        if (!mt.find()) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean checkSelected(JCheckBox chk) {
        return chk.isSelected();
    }

    public static boolean isInteger(String input) {
        try {
            Integer.parseInt(input);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean checkAge(int age) {
        if (age < 18 || age > 65) {
            return false;
        }
        return true;
    }

    public static void setOptionFrame(JFrame frame, String title, boolean resizable) throws Exception {
        frame.setTitle(title);
        frame.setLocationRelativeTo(null);
        frame.setResizable(resizable);
    }

    public static void setOptionFrame(JFrame frame, String title) throws Exception {
        setOptionFrame(frame, title, true);
    }

    public static void setOptionFrame(JFrame frame) throws Exception {
        setOptionFrame(frame, null, true);
    }

    public static void openFrame(JFrame frame, int width, int height) throws Exception {
        new ManipulationFrame(frame, width, height, true).start();
    }

    public static void closeFrame(JFrame frame) throws Exception {
        new ManipulationFrame(frame).start();
    }

    public static void setOptionIFrame(JInternalFrame iFrame, String title, boolean iconifiable, boolean resizable, boolean maximizable, boolean closable) throws Exception {
        iFrame.setTitle(title);
        iFrame.setResizable(resizable);
        iFrame.setClosable(closable);
        iFrame.setIconifiable(iconifiable);
        iFrame.setMaximizable(maximizable);
    }

    public static void setOptionIFrame(JInternalFrame iFrame, String title, boolean iconifiable, boolean resizable, boolean maximizable) throws Exception {
        setOptionIFrame(iFrame, title, iconifiable, resizable, maximizable, false);
    }

    public static void setOptionIFrame(JInternalFrame iFrame, String title, boolean iconifiable, boolean closable) throws Exception {
        setOptionIFrame(iFrame, title, iconifiable, false, false, closable);
    }

    public static void setOptionIFrame(JInternalFrame iFrame, String title, boolean iconifiable) throws Exception {
        setOptionIFrame(iFrame, title, iconifiable, false);
    }

    public static void setOptionIFrame(JInternalFrame iFrame, String title) throws Exception {
        setOptionIFrame(iFrame, title, false);
    }

    public static void setCenterParent(JFrame frame, JInternalFrame iFrame) throws Exception {
        int width = frame.getContentPane().getSize().width;
        int height = frame.getContentPane().getSize().height;
        int iWidth = iFrame.getSize().width;
        int iHeight = iFrame.getSize().height;

        iFrame.setLocation((width - iWidth) / 2, (height - iHeight) / 2);
    }

    public static void addIFrame(JFrame frame, JInternalFrame iFrame, boolean visible) throws Exception {
        setCenterParent(frame, iFrame);
        frame.getContentPane().add(iFrame);
        iFrame.setVisible(visible);
    }

    public static void addIFrame(JFrame frame, JInternalFrame iFrame) throws Exception {
        addIFrame(frame, iFrame, true);
    }

    public static void disposeIFrame(JFrame frame) throws Exception {
        for (Component com : frame.getContentPane().getComponents()) {
            if (com instanceof JInternalFrame) {
                ((JInternalFrame) com).dispose();
            } else {
                frame.getContentPane().remove(com);
            }
        }
    }
    
     //Manipulation with file
    public static boolean createNewFolder(String path) throws Exception {
        return ManipulationFile.createNewFolder(path, false);
    }

    public static boolean deleteFile(String path) throws Exception {
        return ManipulationFile.delete(path);
    }

    public static void copyFile(String pathStart, String pathEnd, String fileName) throws Exception {
        ManipulationFile.copy(pathStart, pathEnd, fileName);
    }

    public static void copyFile(String pathStart, String pathEnd) throws Exception {
        ManipulationFile.copy(pathStart, pathEnd, null);
    }

    public static boolean checkFileExist(String path, boolean createNewFile) throws Exception {
        return ManipulationFile.checkFileExist(path, createNewFile);
    }

    public static String readFromFile(String path) throws Exception {
        return ManipulationFile.readFromFile(path);
    }

    public static void saveToFile(String path, String content, boolean append) throws Exception {
        ManipulationFile.saveToFile(path, content, append);
    }
}
